﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hse_2013_lines_kv
{
    public partial class MainForm : Form
    {
        private Game g; //новый экземпляр класса Game
        private Word currentRound; //новый экземпляр класса Word, используемый для текущего "раунда"
        List<Button> allButtons; //список для кнопок на форме
        private char[] buttonLetters;
        delegate void ClickHandler();
        private bool first;
        private int timeLeft = 120; //счетчик для таймера
        int k = 0;

        private void placeButtons() //расставить кнопки на форме
        {

            allButtons.Add(button1);
            allButtons.Add(button2);
            allButtons.Add(button3);
            allButtons.Add(button4);
            allButtons.Add(button5);
            allButtons.Add(button6);
            allButtons.Add(button7);
            allButtons.Add(button8);
            allButtons.Add(button9);
            allButtons.Add(button10);

            this.Controls.Remove(button1);
            this.Controls.Remove(button2);
            this.Controls.Remove(button3);
            this.Controls.Remove(button4);
            this.Controls.Remove(button5);
            this.Controls.Remove(button6);
            this.Controls.Remove(button7);
            this.Controls.Remove(button8);
            this.Controls.Remove(button9);
            this.Controls.Remove(button10);
            foreach (Button b in allButtons)
            {
                this.Controls.Add(b);
                if (first)
                {
                    b.Click += new EventHandler(elementClicked);
                }
            }
            first = false;


            ClearField();

        }

        private void elementClicked(object sender, EventArgs e) //событие при нажатии на одну из кнопок
        {
            label1.Text += ((Button)sender).Text;
            ((Button)sender).Hide();

        }

        public MainForm(string filepath)
        {
            first = true;
            InitializeComponent();
            buttonLetters = new char[10];
            g = new Game(filepath);
            timer1.Start();
        }


        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MenuForm menu = new MenuForm();
            menu.Show();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (label1.Text == currentRound.Name)
            {
                newRound();
                k++;
            }
            else
            {
                MessageBox.Show("Ошибочка вышла...");
                ClearField();
            }
        }

        private void newRound()
        {        
            currentRound = g.ShowPic();
            label1.Text = "";
            this.pictureBox1.Image = Image.FromFile(currentRound.Path);
            allButtons = new List<Button>();
            placeButtons();
            fillButtons();
        }

        private int FindButton(Button b)
        {
            int res = 0;
            for (int i = 0; i < allButtons.Count; i++)
            {
                if (allButtons[i] == b)
                {
                    res = i;
                }
            }
            return res;
        }

        private void fillButtons()
        {
            Char[] ar = new Char[]{'A', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С',
        'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я'};

            char[] chars = new char[32];

            foreach (Button b in allButtons)
            {
                b.Text = "";
            }

            for (int i = 224, n = 0; i <= 255; i++, n++)
            {
                chars[n] = (char)i; // (char) приводит код к символу
            }


            List<Button> tempList = new List<Button>();
            foreach (Button b in allButtons)
            {
                tempList.Add(b);
            }

            Random rand = new Random();
            int temp;
            foreach (Char c in currentRound.Name)
            {
                temp = rand.Next(0, tempList.Count);
                allButtons[FindButton(tempList[temp])].Text = c.ToString();
                tempList.RemoveAt(temp);
            }
            foreach (Button b in tempList)
            {
                allButtons[FindButton(b)].Text = ar[rand.Next(0, 32)].ToString();
            }

            this.Invalidate();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            newRound();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            ClearField();
        }

        private void ClearField()
        {
            label1.Text = "";

            foreach (Button b in allButtons)
            {
                b.Show();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                timeLeft = timeLeft - 1;
                timeLabel.Text = timeLeft + " seconds";
            }
            else
            {
                timer1.Stop();
                timeLabel.Text = "Время вышло!";
                ResultForm result = new ResultForm(k);
                result.Show();
                this.Hide();

            }
        }

    }
}